package com.livery.app.model.tables;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import com.livery.data.Booking;
import com.livery.data.Client;

public class BookingModel extends AbstractTableModel{
	private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
	private List<Booking> listBooking;
	public List<Booking> getListBooking() {
		return listBooking;
	}


	public void setListBooking(List<Booking> listBooking) {
		this.listBooking = listBooking;
	}

	private String[] columnNames = {
			"Klient",
			"Samochód",
			"Data rezerwacji",
			"Ilośc dni",
	};
	public BookingModel(List<Booking> listBooking){
		this.listBooking = listBooking;
	}
	
	
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return 4;
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return listBooking.size();
	}
	@Override
	public String getColumnName( int columnIndex )
	{
		return columnNames[ columnIndex ];
	}
	@Override
	public Object getValueAt(int row, int col) {
		// TODO Auto-generated method stub
		switch(col){
		case 0 : return listBooking.get(row).getClient().getFirstName() + " " 
		+ listBooking.get(row).getClient().getLastName();
		case 1 : return listBooking.get(row).getCar().getMake() + " " 
		+ listBooking.get(row).getCar().getModel();
		case 2 : return listBooking.get(row).getDateFrom().toString();
		case 3 : return listBooking.get(row).getDays();
		default : throw new RuntimeException("Nie znaleziono kolumny");
		}
	}
	public Booking getBookingAt(int row){
		return listBooking.get(row);
	}
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch(columnIndex)
		{
		case 0 : return Integer.class;
		case 1 : return String.class;
		case 2 : return	String.class;
		case 3 : return String.class;
		case 4 : return Integer.class;
		default : throw new RuntimeException("Nie znaleziono kolumny");
		}
	}
	public void addRow(Booking booking){
		this.listBooking.add(booking);
		TableModelEvent event = new TableModelEvent( this, this.listBooking.size() - 1, this.listBooking.size() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}
	public void removeRow(Client client){
		int index = listBooking.indexOf(client);
		listBooking.remove(client);
		TableModelEvent event = new TableModelEvent( this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );

		for(TableModelListener l: listeners)
		{
			l.tableChanged(event);
		}
	}
	@Override
	public void addTableModelListener( TableModelListener pTableModelListener )
	{
		if( listeners.contains( pTableModelListener ) )
		{
			return;
		}

		listeners.add( pTableModelListener );
	}

	@Override
	public void removeTableModelListener(TableModelListener l)
	{
		listeners.remove(l);
	}
}
